import 'dart:math';
import '../util/PPCryptoHelper.dart';
import '../request/PPRequest.dart';

class PPSaltManager {
    factory PPSaltManager() =>_getInstance();
    static PPSaltManager get instance => _getInstance();
    static PPSaltManager _instance;
    PPSaltManager._internal() {
        // 初始化
    }
    static PPSaltManager _getInstance() {
        if (_instance == null) {
            _instance = new PPSaltManager._internal();
        }
        return _instance;
    }

    String salt = ""; // 盐
    String randomKey = ""; // 随机数
    Map<String, String> requestInfoStorageRequestCryptKey = {};

    getRandomKey() {
        if(this.randomKey.isNotEmpty) {
            return this.randomKey;
        }

        // 先从本地获取
        List<String> dataArray = [];
        final int codeA =  "A".codeUnitAt(0);
        final int codea = 'a'.codeUnitAt(0);
        final int code0 = '0'.codeUnitAt(0);
        int x = 0;
        while (x < 26) {
            if (x < 10) {
                String numberString = String.fromCharCodes(new Iterable.generate(10, (x) => code0 + x));
                dataArray.add(numberString);
            }
            String uppercase = String.fromCharCodes(new Iterable.generate(26, (x) => codeA + x));
            String lowercase = String.fromCharCodes(new Iterable.generate(26, (x) => codea + x));
            dataArray.add(uppercase);
            dataArray.add(lowercase);
            ++x;
        }
        List<String> randomKeyArray = [];
        while (randomKeyArray.length < 8) {
            int randomNumber = Random().nextInt(1001);
            int position = randomNumber % dataArray.length;
            randomKeyArray.add(dataArray[position]);
        }
        String randomKey = randomKeyArray.join('');
        // 生成之后,存储到本地
        this.randomKey = randomKey;
        return randomKey;
    }

     requestExchangeKey() {
        String ranStr = this.getRandomKey();
        PPCryptoHelper.rsaEncrypt(ranStr, 'resource/file/public_key.pem').then((String result) {
            Map<String, String> param = { "ran": result}; 
            PPRequest.request("gpa-gw/unEncryptFunc/exchangeKey", param, (bool isSucceed, dynamic response, dynamic otherInfo) {
                if(isSucceed) {
                    String salt = response["salt"];
                    this.randomKey = salt;
                } else {
                    print("exchangeKey error: " + response["msg"]);
                }
            }, (Map error){
                print("exchangeKey error: " + error["errMsg"]);
            }, isEncryption: false);    
        }).catchError((error){
            print(error);
        }) ;
    }

    Map<String, String> getPbeWithMd5AndDesKeyAndivHexObj() {
        if (requestInfoStorageRequestCryptKey.isNotEmpty) {
            return requestInfoStorageRequestCryptKey;
        }
        Map<String, String> value = PPCryptoHelper.getKeyHexAndIvHexOfPbeWithMd5AndDes(randomKey, salt, 50);
        if (value.isNotEmpty) {
            requestInfoStorageRequestCryptKey = value;
        }
        return value;
    }

    String getPBEWithMd5AndDesEncrypt(data) {
        Map<String, String> keyAndIv = getPbeWithMd5AndDesKeyAndivHexObj();
        String result = PPCryptoHelper.pbeWithMd5AndDesEncrypt2(data, keyAndIv);
        return result;
    }

    String getPBEWithMd5AndDesDecrypt(data) {
        Map<String, String> keyAndIv = getPbeWithMd5AndDesKeyAndivHexObj();
        String result = PPCryptoHelper.pbeWithMd5AndDesDecrypt2(data, keyAndIv);
        return result;
    }

}
 